package com.education.wisdom.service.impl;

import com.education.common.utils.Convert;
import com.education.wisdom.feign.WelcoFeign;
import com.education.wisdom.mapper.LessonResourcesMapper;
import com.education.wisdom.mapper.ResourcesMapper;
import com.education.wisdom.pojo.LessonResources;
import com.education.wisdom.pojo.dto.MineResTableDTO;
import com.education.wisdom.service.LessonResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.education.wisdom.pojo.consts.FileConst.*;

/**
 * 学校课程资源表 服务层实现
 *
 * @author d
 * @date 2020-07-10 09:42:14
 */
@Service
public class LessonResourcesServiceImpl implements LessonResourcesService {

    @Autowired
    private LessonResourcesMapper lessonResourcesMapper;

    @Autowired
    private WelcoFeign welcoFeign;

    @Autowired
    private ResourcesMapper resourcesMapper;

    @Override
    public Integer selectCountByUserId(Integer userId) {
        return lessonResourcesMapper.selectCountByUserId(userId);
    }

    @Override
    public LessonResources selectLessonResourcesByIdOther(Integer id) {
        return lessonResourcesMapper.selectLessonResourcesByIdOther(id);
    }

    //计数教师上传的课件
    @Override
    public Integer selectLessonResourcesCount(Integer userId) {
        return lessonResourcesMapper.selectLessonResourcesCount(userId);
    }

    //查询学校课程资源表信息
    @Override
    public LessonResources selectLessonResourcesById(Integer id) {
        return lessonResourcesMapper.selectLessonResourcesById(id);
    }

    // 查询学校课程资源表列表
    @Override
    public List<LessonResources> selectLessonResourcesList(LessonResources lessonResources) {
        return lessonResourcesMapper.selectLessonResourcesList(lessonResources);
    }

    @Override
    public List<LessonResources> selectLessonResourcesNotUserId(LessonResources lessonResources) {
        return lessonResourcesMapper.selectLessonResourcesNotUserId(lessonResources);
    }

    // 新增学校课程资源表
    @Override
    public int insertLessonResources(LessonResources lessonResources) {
        return lessonResourcesMapper.insertLessonResources(lessonResources);
    }

    //修改学校课程资源表
    @Override
    public int updateLessonResources(LessonResources lessonResources) {
        return lessonResourcesMapper.updateLessonResources(lessonResources);
    }

    //删除学校课程资源表对象
    @Override
    public int deleteLessonResourcesByIds(String ids) {
        return lessonResourcesMapper.deleteLessonResourcesByIds(Convert.toStrArray(ids));
    }

    @Override
    public List<LessonResources> selectByIds(String ids) {
        return lessonResourcesMapper.selectByIds(Convert.toStrArray(ids));
    }

    @Override
    public List<LessonResources> selectLessonBuyByUserId(String userId) {
        return lessonResourcesMapper.selectLessonBuyByUserId(userId);
    }

    @Override
    public Integer selectCountByUserIdAndTime(Integer userId, String time) {
        return lessonResourcesMapper.selectCountByUserIdAndTime(userId, time);
    }

    /**
     * 备课共享资源体系下购买资源后同步到我的资源库默认分组
     *
     * @param resourceId 今软资源库的资源id
     * @param userId     购买资源的老师id
     */
    @Async
    @Override
    @Transactional
    public void lessonsSharedSynchronousMineRes(Integer resourceId, Integer userId) {
        //默认分组的查找 老师id和默认分组类型     图片4/视频5/文档6
        Map<String, Integer> groupMap = welcoFeign.defaultGroupQueryAndReturn(Integer.valueOf(userId));

        //查找资源
        LessonResources lessonResources = lessonResourcesMapper.selectLessonResourcesById(resourceId);
        //目标资源对象
        MineResTableDTO mineResTableDTO = new MineResTableDTO();
        //mine_res_name,mine_res_url,extension,catalog_id,create_by,create_time,res_type
        mineResTableDTO.setMineResName(lessonResources.getRName());
        mineResTableDTO.setMineResUrl(lessonResources.getRUrl());

        //类型 1 文档 4 图片 5 视频     type_id
        if (lessonResources.getTypeId() == 1) {
            handleUrlToSuffix(mineResTableDTO, groupMap.get("6"));//格式
        } else if (lessonResources.getTypeId() == 4) {
            handleUrlToSuffix(mineResTableDTO, groupMap.get("4"));//格式
        } else if (lessonResources.getTypeId() == 5) {
            handleUrlToSuffix(mineResTableDTO, groupMap.get("5"));//格式
        }

        List<MineResTableDTO> mineResTableDTOS = new ArrayList<>();
        mineResTableDTOS.add(mineResTableDTO);
        //我的资源库的资源插入  批量插入
        resourcesMapper.batchInsertMineResTable(mineResTableDTOS);

    }

    //url中处理出来后缀
    public void handleUrlToSuffix(MineResTableDTO res, Integer catalogId) {
        String extension = res.getMineResUrl().replace(res.getMineResUrl().substring(0, res.getMineResUrl().lastIndexOf(".")) + ".", "");
        res.setExtension(extension);
        res.setResType(judgeResType(extension));
        res.setCreateBy("备课共享体系资源同步");
        res.setCatalogId(catalogId);
    }

    //根据文件后缀判断资源类型
    public String judgeResType(String extension) {
        //        1
        if (IMG_FILE.contains(extension)) return "1";
        //        2
        if (VIDEO_FILE.contains(extension)) return "2";
        //        3
        if (DOC_FILE.contains(extension)) return "3";
        return "0";
    }
}